package com.lqq.view;

import com.lqq.api.AuthApi;
import com.lqq.domain.request.LoginByPassRequest;
import com.lqq.domain.response.BaseResponse;
import java.util.concurrent.atomic.AtomicReference;
import lombok.Data;
import sun.misc.BASE64Decoder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;


@Data
public class Log extends JFrame {

  private String uuid;
  private byte[] b;

  public void showLog() throws IOException {

    //打开窗口先发送请求得到响应BaseResponse

    AtomicReference<BaseResponse> captchaResponse = new AtomicReference<>(AuthApi.getCaptcha());
    BASE64Decoder decoder = new BASE64Decoder();
    // Base64解码
    b = decoder.decodeBuffer(captchaResponse.get().getImg());

    //设置窗口
    this.setSize(700, 550);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.setTitle("登录");
    this.setResizable(false);

    //设置画板
    Container contentPane = getContentPane();

    //组件
    //图片
    JLabel lab1 = new JLabel();

    lab1.setLocation(0, 0);
    lab1.setSize(700, 165);

    //文本
    JLabel lab2 = new JLabel("账    号:");
    JLabel lab3 = new JLabel("密    码:");
    JLabel lab4 = new JLabel("验证码:");
    JLabel lab6 = new JLabel();
    lab6.setIcon(new ImageIcon(b));

    lab6.addMouseListener(new MouseListener() {
      @Override
      public void mouseClicked(MouseEvent e) {

        try {
          captchaResponse.set(AuthApi.getCaptcha());
          BASE64Decoder decoder = new BASE64Decoder();
          // Base64解码
          byte[] b = decoder.decodeBuffer(captchaResponse.get().getImg());
          lab6.setIcon(new ImageIcon(b));
        } catch (IOException ex) {
          ex.printStackTrace();
        }

      }

      @Override
      public void mousePressed(MouseEvent e) {

      }

      @Override
      public void mouseReleased(MouseEvent e) {

      }

      @Override
      public void mouseEntered(MouseEvent e) {

      }

      @Override
      public void mouseExited(MouseEvent e) {

      }
    });
    lab2.setSize(50, 25);
    lab3.setSize(50, 25);
    lab4.setSize(50, 25);
    lab6.setSize(120, 48);

    lab2.setLocation(147, 184);
    lab3.setLocation(147, 239);
    lab4.setLocation(147, 293);
    lab6.setLocation(400, 293);

    //输入框
    JTextField tf1 = new JTextField();
    JPasswordField password = new JPasswordField();
    JTextField tf2 = new JTextField();
    String idenurl = "";
    JLabel iden = new JLabel(new ImageIcon(idenurl));

    tf1.setLocation(220, 183);
    tf1.setSize(326, 27);
    tf2.setLocation(220, 296);
    tf2.setSize(169, 27);
    password.setSize(326, 27);
    password.setLocation(220, 237);
    iden.setLocation(439, 296);
    iden.setSize(107, 27);

    //登录按钮
    JButton btn1 = new JButton("登  录");
    btn1.setSize(204, 27);
    btn1.setLocation(281, 355);

    //注册
    JLabel lab5 = new JLabel("没有账号？ 注册");
    lab5.setSize(134, 17);
    lab5.setLocation(316, 400);
    lab5.addMouseListener(new MouseListener() {
      @Override
      public void mouseClicked(MouseEvent e) {
        //跳转到注册页面

      }

      @Override
      public void mousePressed(MouseEvent e) {

      }

      @Override
      public void mouseReleased(MouseEvent e) {

      }

      @Override
      public void mouseEntered(MouseEvent e) {

      }

      @Override
      public void mouseExited(MouseEvent e) {

      }
    });
    //组件添加到画板

    extracted(contentPane, lab1, lab2, lab3, lab4, lab6, tf1, password, tf2, iden, btn1, lab5);

    contentPane.repaint();

    AuthApi.getCaptcha();

    //添加监听器
    ActionListener listener = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        System.out.println("账号" + tf1.getText());
        System.out.println("密码" + password.getText());
        System.out.println("验证码" + tf2.getText());

        try {
          BaseResponse logResponse = AuthApi.login(LoginByPassRequest.builder()
              .accountNo(tf1.getText())
              .password(password.getText())
              .uuid(captchaResponse.get().getUuid())
              .code(tf2.getText())
              .appPlatformType(3)
              .appid("h5ebe58fe7ef505d07")
              .build());

        } catch (Exception ex) {

          try {
            captchaResponse.set(AuthApi.getCaptcha());
            BASE64Decoder decoder = new BASE64Decoder();
            // Base64解码
            byte[] b = decoder.decodeBuffer(captchaResponse.get().getImg());
            lab6.setIcon(new ImageIcon(b));
          } catch (IOException exception) {
            exception.printStackTrace();
          }
        }
      }
    };
    btn1.addActionListener(listener);

    this.repaint();
  }

  private void extracted(Container contentPane, JLabel lab1, JLabel lab2, JLabel lab3, JLabel lab4,
      JLabel lab6, JTextField tf1, JPasswordField password, JTextField tf2, JLabel iden,
      JButton btn1, JLabel lab5) {
    contentPane.add(lab1);
    contentPane.add(lab2);
    contentPane.add(lab3);
    contentPane.add(lab4);
    contentPane.add(lab5);
    contentPane.add(lab6);
    contentPane.add(btn1);
    contentPane.add(tf1);
    contentPane.add(tf2);
    contentPane.add(password);
    contentPane.add(iden);
    contentPane.repaint();
  }


}
